#include "WatermarkManager.h"
#include "log/log.h"
#include "ProductWatermark.h"
#include "WMarkConfig.h"
namespace android
{

    WatermarkManager *WatermarkManager::mSingleton = NULL;

    WatermarkManager *WatermarkManager::getInstance()
    {
        if (mSingleton == NULL)
        {
            mSingleton = new WatermarkManager();
        }
        return mSingleton;
    }

    WatermarkManager::WatermarkManager()
    {
        int queue_size = 15;
        WMarkConfig *wmarkConfig = WMarkConfig::getInstance();
        mMultiChannelDisplay = wmarkConfig->isMultiChannelDisplay();
        if (!wmarkConfig->isEnableWatermark()) {
            mBufferQueue = NULL;
            mReader = NULL;
            mDispatcher = NULL;
            mLogDispatcher = NULL;
        } else {
            mBufferQueue = new BufferQueue<WMarkBuffer>(queue_size);
            mReader = new ProductWatermark(this, mBufferQueue);
            mDispatcher = new DispatcherThread(this, mBufferQueue);
            mLogDispatcher = new DispatcherLogThread(this);
            wmarkConfig->getStrBackgroundColor(&mStrBgColor);
        }
    }

    void WatermarkManager::notifyDataReady()
    {
        if (mDispatcher != NULL)
        {
            mDispatcher->onDataReady();
        }
    }

    void WatermarkManager::startInput()
    {
        if (mDispatcher == NULL || mReader == NULL)
        {
            ALOGE("%s %d obj=NULL\n", __func__, __LINE__);
            return;
        }

        mDispatcher->start();
        mReader->start();
        mLogDispatcher->start();
    }

    void WatermarkManager::stopInput()
    {
        if (mDispatcher == NULL || mReader == NULL)
        {
            ALOGE("%s %d obj=NULL\n", __func__, __LINE__);
            return;
        }

        mReader->stop();
        mDispatcher->stop();
        mLogDispatcher->stop();
    }

    void WatermarkManager::onDispatcherResult(WMarkBuffer *wMarkBuf) {
        if (!mResultCallback) {
            ALOGW("mResultCallback == NULL, no return watermark result.\n");
            return;
        }
        WaterMarkImage result;
        result.enable = wMarkBuf->enableWmark;
        result.id = wMarkBuf->cameraId;
        result.h = wMarkBuf->getDataHeight();
        result.w = wMarkBuf->getDataWidth();
        result.display_x = wMarkBuf->displayX;
        result.display_y = wMarkBuf->displayY;
        result.image = wMarkBuf->Data();
        result.multiChannel = mMultiChannelDisplay;
        result.strBgColor.enable = mStrBgColor.enable;
        result.strBgColor.r = mStrBgColor.r;
        result.strBgColor.g = mStrBgColor.g;
        result.strBgColor.b = mStrBgColor.b;
        result.strBgColor.a = mStrBgColor.a;
        mResultCallback->onWaterMarkResult(result);
    }

    void WatermarkManager::onDispatcherLogResult(int id, int imgW, int imgH, int x, int y, uint8_t *logImg, int channel, int enable) {
        if (!mResultCallback) {
            ALOGW("mResultCallback == NULL, no return watermark log result.\n");
            return;
        }
        WaterMarkImage result;
        result.enable = enable;
        result.id = id;
        result.h = imgH;
        result.w = imgW;
        result.display_x = x;
        result.display_y = y;
        result.logImage = logImg;
        result.channel = channel;
        result.multiChannel = mMultiChannelDisplay;
        mResultCallback->onWaterMarkLogResult(result);
    }

}
